import request from "@/utils/request";

/**
 * 好友信息接口
 */
export interface Friend {
  id: number;
  username: string;
  avatar?: string;
  level: number;
  status: "online" | "offline" | "playing" | "away";
  lastActive?: string;
  currentGame?: string;
  remark?: string;
  friendshipDate?: string;
  isFavorite?: boolean;
}

/**
 * 好友请求接口
 */
export interface FriendRequest {
  id: number;
  senderId: number;
  senderName: string;
  senderAvatar?: string;
  receiverId: number;
  status: "pending" | "accepted" | "rejected";
  requestTime: string;
  message?: string;
}

/**
 * 消息接口
 */
export interface Message {
  id: number;
  senderId: number;
  senderName: string;
  senderAvatar?: string;
  receiverId: number;
  content: string;
  messageType: "text" | "image" | "voice" | "game_invite" | "achievement";
  sendTime: string;
  isRead: boolean;
  relatedGameId?: number;
  relatedGameName?: string;
}

/**
 * 游戏邀请接口
 */
export interface GameInvite {
  id: number;
  senderId: number;
  senderName: string;
  receiverId: number;
  gameId: number;
  gameName: string;
  inviteTime: string;
  status: "pending" | "accepted" | "rejected" | "expired";
  message?: string;
}

/**
 * 社区动态接口
 */
export interface SocialActivity {
  id: number;
  userId: number;
  username: string;
  userAvatar?: string;
  activityType: "achievement" | "level_up" | "game_complete" | "new_game";
  content: string;
  gameId?: number;
  gameName?: string;
  activityTime: string;
  likes: number;
  comments: number;
  hasLiked?: boolean;
}

/**
 * 社交API
 */
export default {
  /**
   * 获取好友列表
   * @param userId 用户ID
   * @returns 好友列表
   */
  getFriendList(userId: number) {
    return request({
      url: `/api/v1/social/friends`,
      method: "get",
      params: { userId },
    });
  },

  /**
   * 获取好友请求列表
   * @param userId 用户ID
   * @returns 好友请求列表
   */
  getFriendRequests(userId: number) {
    return request({
      url: `/api/v1/social/friend-requests`,
      method: "get",
      params: { userId },
    });
  },

  /**
   * 发送好友请求
   * @param data 请求数据
   * @returns 请求结果
   */
  sendFriendRequest(data: { senderId: number; receiverId: number; message?: string }) {
    return request({
      url: `/api/v1/social/friend-requests`,
      method: "post",
      data,
    });
  },

  /**
   * 处理好友请求
   * @param requestId 请求ID
   * @param action 操作(accept/reject)
   * @returns 操作结果
   */
  handleFriendRequest(requestId: number, action: "accept" | "reject") {
    return request({
      url: `/api/v1/social/friend-requests/${requestId}`,
      method: "put",
      data: { action },
    });
  },

  /**
   * 删除好友
   * @param userId 用户ID
   * @param friendId 好友ID
   * @returns 操作结果
   */
  removeFriend(userId: number, friendId: number) {
    return request({
      url: `/api/v1/social/friends/${friendId}`,
      method: "delete",
      params: { userId },
    });
  },

  /**
   * 获取消息列表
   * @param userId 用户ID
   * @param params 查询参数
   * @returns 消息列表
   */
  getMessages(userId: number, params?: { page?: number; size?: number; contactId?: number }) {
    return request({
      url: `/api/v1/social/messages`,
      method: "get",
      params: { userId, ...params },
    });
  },

  /**
   * 获取消息会话列表
   * @param userId 用户ID
   * @returns 会话列表
   */
  getConversations(userId: number) {
    return request({
      url: `/api/v1/social/conversations`,
      method: "get",
      params: { userId },
    });
  },

  /**
   * 发送消息
   * @param data 消息数据
   * @returns 操作结果
   */
  sendMessage(data: {
    senderId: number;
    receiverId: number;
    content: string;
    messageType?: string;
  }) {
    return request({
      url: `/api/v1/social/messages`,
      method: "post",
      data,
    });
  },

  /**
   * 标记消息为已读
   * @param messageId 消息ID或消息ID数组
   * @returns 操作结果
   */
  markMessageRead(messageId: number | number[]) {
    return request({
      url: `/api/v1/social/messages/read`,
      method: "put",
      data: { messageId },
    });
  },

  /**
   * 发送游戏邀请
   * @param data 邀请数据
   * @returns 操作结果
   */
  sendGameInvite(data: { senderId: number; receiverId: number; gameId: number; message?: string }) {
    return request({
      url: `/api/v1/social/game-invites`,
      method: "post",
      data,
    });
  },

  /**
   * 处理游戏邀请
   * @param inviteId 邀请ID
   * @param action 操作(accept/reject)
   * @returns 操作结果
   */
  handleGameInvite(inviteId: number, action: "accept" | "reject") {
    return request({
      url: `/api/v1/social/game-invites/${inviteId}`,
      method: "put",
      data: { action },
    });
  },

  /**
   * 获取社区动态
   * @param userId 用户ID
   * @param params 查询参数
   * @returns 社区动态列表
   */
  getSocialActivities(
    userId: number,
    params?: { page?: number; size?: number; friendsOnly?: boolean }
  ) {
    return request({
      url: `/api/v1/social/activities`,
      method: "get",
      params: { userId, ...params },
    });
  },

  /**
   * 点赞社区动态
   * @param activityId 动态ID
   * @param userId 用户ID
   * @returns 操作结果
   */
  likeActivity(activityId: number, userId: number) {
    return request({
      url: `/api/v1/social/activities/${activityId}/like`,
      method: "post",
      data: { userId },
    });
  },
};
